package cn.fl.customer.service.impl;

import java.util.ArrayList;
import java.util.List;

import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysUsrApiService;
import cmm.user.facade.system.condition.SysUsrQueryCondition;
import cmm.user.facade.system.dto.SysUsrDTO;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.fl.customer.service.ICstManualRelationService;
import cn.fl.customer.transform.CstManualRelationVOTransform;
import cn.fl.customer.vo.CstManualRelationVO;
import com.alibaba.fastjson.JSONObject;
import fl.customer.facade.customer.api.ICstManualRelationApiService;
import fl.customer.facade.customer.condition.CstManualRelationQC;
import fl.customer.facade.customer.dto.CstManualRelationDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.fl.framework.base.context.CurrentThreadContext;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: 信审添加与客户、供应商、挂靠公司有相关的人员 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class CstManualRelationServiceImpl implements ICstManualRelationService {

	/**
	 * 日志
	 */
		private static final Logger logger = LoggerFactory.getLogger(CstManualRelationServiceImpl.class);

	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private ICstManualRelationApiService cstManualRelationApiService;
	@Autowired
	private ISysUsrApiService sysUsrApiService;


	@Override
	public CstManualRelationVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return CstManualRelationVOTransform.toVO(this.cstManualRelationApiService.selectById(id));
	}

	@Override
	public Long add(CstManualRelationVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return this.cstManualRelationApiService.add(CstManualRelationVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.cstManualRelationApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
	@Override
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(CstManualRelationVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.cstManualRelationApiService.modify(CstManualRelationVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page) {
			page = new Pager();
		}
		Pager pager = this.cstManualRelationApiService.searchListPage(page);
		if (CollectionUtils.isNotEmpty(pager.getResultList())) {
			List<CstManualRelationVO> voList = CstManualRelationVOTransform.toVOList((List<CstManualRelationDTO>) pager.getResultList());
			pager.setResultList(voList);
			this.setDictData(voList);
		}
		return pager;
	}

	/**
	 * 添加数据字典名称
	 *
	 * @param voList
	 */
	public void setDictData(List<CstManualRelationVO> voList) {
		if (CollectionUtils.isEmpty(voList)){
			return;
		};
		List<Long> idList=new ArrayList<Long>();
		List<SysUsrDTO> userDtl =null;
		for (CstManualRelationVO cstManualRelationVO : voList) {
			if(StringUtils.isNotEmpty(cstManualRelationVO.getMdfUsrId())&& !idList.contains(cstManualRelationVO.getMdfUsrId())){
				idList.add(cstManualRelationVO.getMdfUsrId());
			}
			if(StringUtils.isNotEmpty(cstManualRelationVO.getCrtUsrId())&& !idList.contains(cstManualRelationVO.getCrtUsrId())){
				idList.add(cstManualRelationVO.getCrtUsrId());
			}
		}

		if(CollectionUtils.isNotEmpty(idList)){
			//去重
			SysUsrQueryCondition sysUsrQC = new SysUsrQueryCondition();
			sysUsrQC.setSysCd(CurrentThreadContext.getCurrentSysCd());
			sysUsrQC.setUsrIds(idList);
			//查询系统用户
			try {
				userDtl = this.sysUsrApiService.searchSysUsrList(sysUsrQC);
			} catch (Exception e) {
				BaseLogUtils.info(logger, "searchSysUsrList", "查询用户系统数据失败！[error]:"+e.toString()+"======>DATA:"+ JSONObject.toJSONString(userDtl),CurrentThreadContext.getCurrentUserName());
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_605_NAME,BaseExceptionConstant.BASE_EXCEPTION_CODE_605_NAME);
			}
		}

		if(CollectionUtils.isNotEmpty(userDtl)){
			for (CstManualRelationVO cstManualRelationVO : voList) {
				for (SysUsrDTO sysUsrDTO : userDtl) {
					if(StringUtils.isNotEmpty(cstManualRelationVO.getMdfUsrId())){
						if(cstManualRelationVO.getMdfUsrId().equals(sysUsrDTO.getId())){
							cstManualRelationVO.setMdfUsrNm(StringUtils.isNotEmpty(sysUsrDTO.getRefRlNm())?sysUsrDTO.getRefRlNm():"");
						}
					}
					if(StringUtils.isNotEmpty(cstManualRelationVO.getCrtUsrId())){
						if(cstManualRelationVO.getCrtUsrId().equals(sysUsrDTO.getId())){
							cstManualRelationVO.setCrtUsrNm(StringUtils.isNotEmpty(sysUsrDTO.getRefRlNm())?sysUsrDTO.getRefRlNm():"");
						}
					}
				}

			}
		}
	}
	
		@Override
	public List<CstManualRelationVO> searchList(CstManualRelationQC QC) throws BaseException{
		 if(null == QC){
		     //TODO 这里的判断不是很严谨 有时间详细处理
             throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
         }
         return CstManualRelationVOTransform.toVOList(this.cstManualRelationApiService.searchList(QC));
		
	}
}
